﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;

namespace WpfApplication1 {
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {

        int lastX=0;
        int lastY=0;
        Bitmap bm = new Bitmap(500, 500);
         
        public MainWindow() {
            InitializeComponent();
            Graphics g = Graphics.FromImage(bm);
            g.DrawEllipse(new System.Drawing.Pen(new System.Drawing.SolidBrush(System.Drawing.Color.Red)), 10, 10, 10, 10);
            System.IO.MemoryStream m = new System.IO.MemoryStream();
            bm.Save(m, System.Drawing.Imaging.ImageFormat.Bmp);

         // System.Drawing.Image img=  System.Drawing.Image.FromStream(m);

          BitmapImage bmi = new BitmapImage();
          bmi.BeginInit();
          bmi.StreamSource = m;
          bmi.EndInit();

          i1.Source = bmi;

          Canvas c = new Canvas();
          }

        
        private void i1_MouseMove(object sender, MouseEventArgs e) {


            if (e.LeftButton == MouseButtonState.Pressed) {
                double d = Math.Sqrt(Math.Pow((int)e.GetPosition(i1).X - lastX,2) + Math.Pow((int)e.GetPosition(i1).Y - lastY,2));
                Graphics g = Graphics.FromImage(bm);

                if(d>10)
                g.DrawLine(new System.Drawing.Pen(new System.Drawing.SolidBrush(System.Drawing.Color.Red),10), (int)e.GetPosition(i1).X, (int)e.GetPosition(i1).Y, lastX, lastY);
                else
                g.FillEllipse(new System.Drawing.SolidBrush(System.Drawing.Color.Red), (int)e.GetPosition(i1).X, (int)e.GetPosition(i1).Y, 10, 10);

                lastX = (int)e.GetPosition(i1).X;
                lastY = (int)e.GetPosition(i1).Y;

                System.IO.MemoryStream m = new System.IO.MemoryStream();
                bm.Save(m, System.Drawing.Imaging.ImageFormat.Bmp);

                // System.Drawing.Image img=  System.Drawing.Image.FromStream(m);

                BitmapImage bmi = new BitmapImage();
                bmi.BeginInit();
                bmi.StreamSource = m;
                bmi.EndInit();

                i1.Source = bmi;



            }
        }
    }
}
